import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PaintCS232 extends JFrame{
	
	private Canvas canvas;
	private ToolBox toolBox;
	private ColorBox colorBox;
	private ItemHandler itemHandler;
	private Container c;
	private JMenuBar bar;
	private JMenu fileMenu, editMenu, helpMenu;
	private JMenuItem newFile, openFile, saveFile, exit, undo, editColor, help, about;
	private JPanel paintPanel;
	
	public PaintCS232() 	{
		c = getContentPane();
		c.setLayout(new BorderLayout());
		itemHandler = new ItemHandler();
		setJMenuBar(bar = new JMenuBar());
		addMenus();
		
		toolBox = new ToolBox();
		colorBox = new ColorBox();
		canvas = new Canvas(toolBox, colorBox);
		
		paintPanel = new JPanel();
		paintPanel.setLayout(new BorderLayout());
		paintPanel.setPreferredSize(new Dimension(54,600));
		paintPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		paintPanel.add(toolBox, BorderLayout.NORTH);
		paintPanel.add(colorBox, BorderLayout.CENTER);
		
		c.add(canvas, BorderLayout.CENTER);
		c.add(paintPanel, BorderLayout.WEST);
		c.add(canvas.statusPane, BorderLayout.SOUTH);
		
		setTitle("Paint");
		setResizable(false);
		setPreferredSize(new Dimension(800,620));
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		addKeyListener(itemHandler);
		pack();
	}
	
	public void addMenus() {
		(fileMenu = new JMenu("File")).setMnemonic('F');
		(editMenu = new JMenu("Edit")).setMnemonic('E');
		(helpMenu = new JMenu("Help")).setMnemonic('H');
		
		newFile = new JMenuItem("New",'N');
		newFile.addActionListener(itemHandler);
		fileMenu.add(newFile);
		
		openFile = new JMenuItem("Open...",'O');
		openFile.addActionListener(itemHandler);
		fileMenu.add(openFile);
		
		saveFile = new JMenuItem("Save AS...",'S');
		saveFile.addActionListener(itemHandler);
		fileMenu.add(saveFile);
		
		fileMenu.addSeparator();
		
		exit = new JMenuItem("Exit",'X');
		exit.addActionListener(itemHandler);
		fileMenu.add(exit);
		
		undo = new JMenuItem("Undo Ctrl+Z", 'U');
		undo.addActionListener(itemHandler);
		editMenu.add(undo);
		
		editColor = new JMenuItem("Edit Color...",'C');
		editColor.addActionListener(itemHandler);
		editMenu.add(editColor);
		
		help = new JMenuItem("Help", 'H');
		help.addActionListener(itemHandler);
		helpMenu.add(help);
		
		helpMenu.addSeparator();
		
		about = new JMenuItem("About Paint",'A');
		about.addActionListener(itemHandler);
		helpMenu.add(about);
		
		bar.add(fileMenu);
		bar.add(editMenu);
		bar.add(helpMenu);
	}
	
	public static void main(String [] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new PaintCS232().setVisible(true);
			}
		});
	}
	
	private class ItemHandler implements ActionListener, KeyListener{
		
		public void actionPerformed( ActionEvent e ) {
			boolean status = false;
			if(e.getSource()==newFile) {
				if(!canvas.shapes.isEmpty()) {
					int response = JOptionPane.showConfirmDialog(c,"Would you like to save current File?",
																   "Save ", JOptionPane.YES_NO_CANCEL_OPTION);
					if(response==0) { 
						status = canvas.saveFile();
						if(!status) 
							JOptionPane.showMessageDialog(c,"Error saving file!!", "File Save Error", JOptionPane.ERROR_MESSAGE);
						canvas.clearCanvas();
						canvas.repaint();
					}
					else if(response==1) {
						canvas.clearCanvas();
						canvas.repaint();
					}
				}
				else {
					canvas.clearCanvas();
					canvas.repaint();
				}
			}
			else if(e.getSource()==openFile) {
				if(!canvas.shapes.isEmpty()) {
					int response = JOptionPane.showConfirmDialog(c,"Would you like to save current File?",
																   "Save ", JOptionPane.YES_NO_CANCEL_OPTION);
					if(response==0) { 
						status = canvas.saveFile();
						if(!status) 
							JOptionPane.showMessageDialog(c,"Error saving file!!", "File Save Error", JOptionPane.ERROR_MESSAGE);
						status = canvas.openFile();
						if(!status) 
							JOptionPane.showMessageDialog(c,"Error opening file!!", "File Open Error", JOptionPane.ERROR_MESSAGE);
					}
					else if(response==1) {
						status = canvas.openFile();
						if(!status) 
							JOptionPane.showMessageDialog(c,"Error opening file!!", "File Open Error", JOptionPane.ERROR_MESSAGE);
					}
				}
				else {
					status = canvas.openFile();
					if(!status) 
						JOptionPane.showMessageDialog(c,"Error opening file!!", "File Open Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(e.getSource()==saveFile) {
				status = canvas.saveFile();
				if(!status) 
					JOptionPane.showMessageDialog(c,"Error saving file!!", "File Save Error", JOptionPane.ERROR_MESSAGE);
			}
			else if(e.getSource()==exit) { System.exit(0); }
			else if(e.getSource()==undo) { canvas.undoLastShape(); }
			else if(e.getSource()==editColor) { colorChooser(); }
			else if(e.getSource()==help) {}
			else if(e.getSource()==about) {
				JOptionPane.showMessageDialog(c,
					"Created by Group ?:\n"+
					"Group Leader: Ryan Vargas \n"+
					"David Carl Fleming \n"+
					"Justin Quinn \n" +
					"Matthew Shaffer",
					"About Paint", JOptionPane.PLAIN_MESSAGE);
			}
			
			
		}
		
		public void keyPressed(KeyEvent e) {
			if((e.getKeyCode() == KeyEvent.VK_Z) && (e.isControlDown())) {
				canvas.undoLastShape();
			}
		}
		
		public void keyReleased(KeyEvent e) {}
		public void keyTyped(KeyEvent e) {}
		
		public void colorChooser() {
			final JColorChooser colorChooser = new JColorChooser(colorBox.colorPreview.getBackground());
			ActionListener okActionListener = new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {
					colorBox.colorPreview.setBackground(colorChooser.getColor());
					canvas.setForeGroundColor(colorChooser.getColor());
				}
			};
			ActionListener cancelActionListener = new ActionListener() {
				public void actionPerformed(ActionEvent actionEvent) {}
			};
			final JDialog dialog = JColorChooser.createDialog(c, "Change Color", true,
				colorChooser, okActionListener, cancelActionListener);
			dialog.setVisible(true);
		}
	}
}
